
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author rohit
 */
public class UpdateThread1 extends MainGui implements Runnable{

   // private Connection connection = null;
    int first;
    int last;
    Connection c;
    public UpdateThread1(int firstinsert, int lastinsert, Connection connection1) {
        this.first = firstinsert;
        this.last = lastinsert;
        this.c=connection1;
        establishConnection();
        
    }

    @Override
    public void establishConnection() {
        System.out.println("-------- PostgreSQL "
                + "JDBC Connection Testing ------------");

        if (connection != null) {
            return;
        }
        try {
            Class.forName("org.postgresql.Driver");
            connection = DriverManager.getConnection(
                    "jdbc:postgresql://localhost:5432/test", "rohit",
                    "d3fad7d3");
            if (connection != null) {
                System.out.println("Connecting to database... going for update data");
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
    
    @Override
        public void startTx() {
        PreparedStatement prep = null;
        try {
            prep = connection.prepareStatement("begin transaction;");
            prep.executeUpdate();
            //System.out.println("begin transaction");
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    @Override
    public void setEnvironment() {
        PreparedStatement prep = null;
        try {
            prep = connection.prepareStatement("set enable_seqscan = off");
            prep.executeUpdate();
            prep = connection.prepareStatement("set enable_bitmapscan = off");
            prep.executeUpdate();
            System.out.println("Environment variable set");
        } catch (Exception e) {
            System.out.println("Problem in setting the environment variables");
        }
    }

    @Override
    public void endTx() {
        PreparedStatement prep = null;
        try {
            prep = connection.prepareStatement("end transaction;");
            prep.executeUpdate();
           // System.out.println("End transaction");
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    
    @Override
    public PreparedStatement updateDataMBI(int firstUpdate, int lastUpdate) {
        ResultSet rs = null;
        Statement s = null;
        PreparedStatement prep = null;
        
        int valueOfId;
        try {
            connection.setAutoCommit(false);
            for (valueOfId = firstUpdate; valueOfId < lastUpdate; valueOfId++) {
                //startTx();
                String updateSql = "UPDATE emp SET name = 'goyal' where id = " + Integer.toString(valueOfId);
                Statement st = connection.createStatement();
                st.executeUpdate(updateSql);
                //endTx();
            }
            System.out.println("Update works fine");
            connection.commit();
        } catch (Exception e) {
            System.out.println(e);
        }
        return prep;
    }
    
    
    @Override
    public void run() {
        updateDataMBI(first,last);
    }
}
